package com.hulk.ratel.notify.control;




import com.hulk.ratel.common.util.IDWorkerRadix;
import com.hulk.ratel.notify.service.INotifyWxPayService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Description: 接收处理微信通知
 * @author hulk
 * @date 2017-07-05
 * @version V1.0
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/notify/v1/api")
public class NotifyWxPayController {


	private INotifyWxPayService notifyPayService;

	/**
	 * 微信支付(统一下单接口)后台通知响应
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
     */
	@RequestMapping("/WXPAY_CALLBACK")
	@ResponseBody
	public String wxPayNotifyResp(HttpServletRequest request) throws  IOException {
		log.debug("====== 开始接收微信支付回调通知 ======");
		String workId = (IDWorkerRadix.getId());
		String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
		log.info("【微信支付回调通知】通知请求数据:reqStr={},workId={}", xmlResult,workId);
		String notifyResp = notifyPayService.doWxPayNotify(xmlResult,workId);
		log.info("响应给微信:{},workId={}", notifyResp,workId);
		log.debug("====== 完成接收微信支付回调通知 ======");
		return notifyResp;
	}



}
